package com.example.helpconnect;

import java.util.ArrayList;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.view.View.OnClickListener;

import com.example.controller.*;
import com.example.model.*;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.OverlayItem;

public class GoogleMapsOverlay extends ItemizedOverlay<OverlayItem>{
    private ArrayList<OverlayItem> mapOverlays = new ArrayList<OverlayItem>();
    private Context context;
    
    public GoogleMapsOverlay(Drawable defaultMarker) {
        super(boundCenterBottom(defaultMarker));
        // TODO Auto-generated constructor stub
    }

    public GoogleMapsOverlay(Drawable defaultMarker, Context context) {
        this(defaultMarker);
        this.context = context;
        // TODO Auto-generated constructor stub
    }
    
    @Override
    protected OverlayItem createItem(int i){
        return mapOverlays.get(i);
    }
    
    @Override
    public int size(){
        return mapOverlays.size();
    }
    
    @Override
    protected boolean onTap(int index){
        final OverlayItem item = mapOverlays.get(index);
        AlertDialog.Builder dialog = new AlertDialog.Builder(context);
        dialog.setTitle(item.getTitle());
        dialog.setMessage(item.getSnippet());
        dialog.setNegativeButton("Close", null);
        dialog.setPositiveButton("Request", new DialogInterface.OnClickListener() {
            
			@Override
			public void onClick(DialogInterface arg0, int arg1) {
				String itemSnippet = item.getSnippet();
				String[] splitSnippet = itemSnippet.split("service code: ");
				if(itemSnippet.contains("service code: ") && splitSnippet.length > 0){
					String newServiceStatusRequestDetails = splitSnippet[splitSnippet.length-1];
					String[] splitNewServiceStatusRequestDetails = newServiceStatusRequestDetails.split("-");
					
					//The service code in the snippet of the overlay represents (ServiceID-ServiceOwnerID-RequesterUserID). 
		        	//This allows us to send these parameters to the overlay in order to create the service request in
		        	//in the backend.
					int serviceID = Integer.parseInt(splitNewServiceStatusRequestDetails[0]);
					int serviceOwnerID = Integer.parseInt(splitNewServiceStatusRequestDetails[1]);
					int serviceRequesterID = Integer.parseInt(splitNewServiceStatusRequestDetails[2]);
					
					if(serviceOwnerID != serviceRequesterID){
						ServiceStatus newServiceStatus = new ServiceStatus(new ServiceStatusCRUD());
						
						newServiceStatus.setServiceID(serviceID);
						newServiceStatus.setServiceOwnerID(serviceOwnerID);
						newServiceStatus.setRequesterUserID(serviceRequesterID);
						
						newServiceStatus.create();
						
						System.out.println("Request service!\nTitle: " + item.getTitle() + "\nSnippet: " + item.getSnippet());
						
						//Send the alerting SMS
						sendServiceRequestSMS(serviceID, serviceOwnerID, serviceRequesterID);
					}
					else{
						System.out.println("Not able to perform request.");
						AlertDialog.Builder dialog = new AlertDialog.Builder(context);
				        dialog.setTitle("Invalid Request");
				        dialog.setMessage("You can not request your own service");
				        dialog.setNeutralButton("Close", null);
				        dialog.show();
					}
				
				}
				else
				{
					System.out.println("Not able to perform request.");
					AlertDialog.Builder dialog = new AlertDialog.Builder(context);
			        dialog.setTitle("Invalid Request");
			        dialog.setMessage("You can not perform a request. (This is your location)");
			        dialog.setNeutralButton("Close", null);
			        dialog.show();
				}
			}
		});
        
        dialog.show();
        return true;
    }
    
    public void sendServiceRequestSMS(int serviceID, int serviceOwnerID, int serviceRequesterID){
    	
    	
    	Service service = new Service(new ServiceCRUD());
    	service = service.read(serviceID);
    	
    	User user = new User(new UserCRUD());
    	
    	User sendingUser = user.read(serviceRequesterID);
    	
    	String messageHeading = "**HelpConnect Message**\n";
    	String message = messageHeading + sendingUser.getFirstName() + " " + sendingUser.getLastName() + " has requested your service: \n\"" +
    			service.getDescription() + "\"\nPlease log into HelpConnect to review this request.";
    	
    	SMSUtility smsTool = new SMSUtility(serviceOwnerID, message);
    	smsTool.sendSMS();
    	
    	
    }
    
    public void addOverlay(OverlayItem overlay){
        mapOverlays.add(overlay);
        this.populate();
    }
}
